package com.cty.dijkstra;

import java.util.Arrays;

public class graph {
    char[] vertexs;
    int[][] matrix;
    VisitedVertex vv;

    //构造器
    public graph(char[] vertexs,int[][] martix) {
        this.vertexs = vertexs;
        this.matrix = martix;
    }

    public void showGraph() {
        for (int[] link: matrix) {
            System.out.println(Arrays.toString(link));
        }
    }

    public void dsj(int index) {
        vv = new VisitedVertex(index,vertexs.length);
        update(index);
        for (int j = 1;j < vertexs.length;j++) {
            index = vv.updateArr();
            update(index);
        }
    }

    private void update(int index) {
        int len;
        for (int j = 0;j < matrix[index].length;j++) {
            len = vv.getDis(index) + matrix[index][j];
            if (!vv.in(j) && len < vv.getDis(j)) {
                vv.updateDis(j,len);
                vv.updatePre(index,j);
            }
        }
    }

    public void showRes() {
        vv.showRes();
    }
}
